// MyXMLStream.cpp: implementation of the CMyXMLStream class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "share.h"
#include "MyXMLStream.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMyXMLStream::CMyXMLStream()
{
	m_pbuffer = NULL;
	m_bufferSize = 0;
	m_insertPoint = 0;
	m_bufferIncreaseSize = 1024*1024;
}

CMyXMLStream::~CMyXMLStream()
{
	if(m_pbuffer)
		free(m_pbuffer);
}

CMyXMLStream& CMyXMLStream::operator << (char *pData)
{
	long dataSize = strlen( pData );
	if( (m_bufferSize - m_insertPoint) > dataSize )
	{
		memcpy( m_pbuffer+m_insertPoint, pData, dataSize );
		m_insertPoint += dataSize;
	}
	else
	{
		Increasebuffer();
		*this << pData;
	}
	return *this;
}

CMyXMLStream& CMyXMLStream::operator << (string& data)
{
	long dataSize = data.size();
	if( (m_bufferSize - m_insertPoint) > dataSize )
	{
		memcpy( m_pbuffer+m_insertPoint, data.c_str(), dataSize );
		m_insertPoint += dataSize;
	}
	else
	{
		Increasebuffer();
		*this << data;
	}
	return *this;
}

CMyXMLStream& CMyXMLStream::operator << (CMyXMLStream& Stream)
{
	long dataSize = Stream.GetDataSize();
	if( (m_bufferSize - m_insertPoint) > dataSize )
	{
		memcpy( m_pbuffer+m_insertPoint, Stream.Data(), dataSize );
		m_insertPoint += dataSize;
	}
	else
	{
		Increasebuffer();
		*this << Stream;
	}
	return *this;
}

void CMyXMLStream::Clear()
{
	if(m_pbuffer)
		free(m_pbuffer);
	
	m_pbuffer = NULL;
	m_bufferSize = 0;
	m_insertPoint = 0;
}

long CMyXMLStream::GetDataSize()
{
	return m_insertPoint;
}

char * CMyXMLStream::Data()
{
	return m_pbuffer;
}

void CMyXMLStream::Increasebuffer()
{
	m_pbuffer = (char *)realloc(m_pbuffer,m_bufferSize + m_bufferIncreaseSize);
	m_bufferSize += m_bufferIncreaseSize;
}

void CMyXMLStream::SetBufferIncreaseSize(long newSize)
{
	m_bufferIncreaseSize = newSize;
}
